using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace SeeqSharp
{
    [XmlRoot(ElementName="playlist")]
    public class Playlist
    {
        private const string SearchUri = "http://www.seeqpod.com/api/music/anonSearchPaginationFlash?s={0}&n=100&q={1}";
        private const string GetPlaylistUri = "http://www.seeqpod.com/api/music/getPlaylist?playlist_id={0}";
        private const string PlaylistUri = "http://www.seeqpod.com/search/?plid=";

        private string title = "";
        private List<Track> trackList;
        private int startIndex;
        private int endIndex;
        private int totalIndices;

        public Playlist()
        {
            this.trackList = new List<Track>();
        }

        [XmlElement(ElementName = "title")]
        public string Title
        {
            get { return (this.title); }
            set { this.title = value; }
        }

        [XmlArray(ElementName = "trackList")]
        [XmlArrayItem(ElementName = "track")]
        public List<Track> TrackList
        {
            get { return (this.trackList); }
            set { this.trackList = value; }
        }

        [XmlAttribute(AttributeName = "start")]
        public int StartIndex
        {
            get { return (this.startIndex); }
            set { this.startIndex = value; }
        }

        [XmlAttribute(AttributeName = "end")]
        public int EndIndex
        {
            get { return (this.endIndex); }
            set { this.endIndex = value; }
        }

        [XmlAttribute(AttributeName = "total")]
        public int TotalIndices
        {
            get { return (this.totalIndices); }
            set { this.totalIndices = value; }
        }

        private static Playlist CreateFromUri(string uri)
        {
            XmlTextReader reader = new XmlTextReader(uri);
            reader.Namespaces = false;
            XmlSerializer serializer = new XmlSerializer(typeof(Playlist));
            Playlist newPlaylist = (Playlist)serializer.Deserialize(reader);
            reader.Close();
            return (newPlaylist);
        }

        public static Playlist FromPlaylistID(string playlistId)
        {
            return (Playlist.CreateFromUri(string.Format(Playlist.GetPlaylistUri, playlistId)));
        }

        public static Playlist FromPlaylistUrl(string playlistUrl)
        {
            string playlistId = playlistUrl.Remove(0, PlaylistUri.Length);
            return (Playlist.CreateFromUri(string.Format(Playlist.GetPlaylistUri, playlistId)));
        }

        public static Playlist Search(string text)
        {
            int i = 0;
            Playlist finalPlaylist = null;
            do
            {
                Playlist tmpPlaylist = Playlist.CreateFromUri(string.Format(Playlist.SearchUri, i, text));
                if (finalPlaylist == null)
                    finalPlaylist = tmpPlaylist;
                else
                    finalPlaylist.TrackList.AddRange(tmpPlaylist.TrackList);
                i = tmpPlaylist.EndIndex + 1;
            }
            while (finalPlaylist.TrackList.Count < finalPlaylist.TotalIndices);

            return (finalPlaylist);
        }
    }
}
